package one.bugu.android.demon.ui.fragment.snatch;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.snatch.SnatchPeriodBean;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.common.MyBaseFragment;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.activity.snatch.SnatchDetailsActivty;
import one.bugu.android.demon.ui.adapter.snatch.SnatchListAdapter;
import one.bugu.android.demon.ui.refresh.SmartRefreshLayout;
import one.bugu.android.demon.ui.refresh.listener.OnRefreshListener;
import one.bugu.android.demon.ui.refresh.listener.OnRefreshLoadmoreListener;
import one.bugu.android.demon.ui.refresh.listener.RefreshLayout;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.PreferencesUtil;

/**
 * Description BuyingFragment 抢购结束fragment
 *
 * @author liuxin
 * @date 2018/10/26 14:14
 */
@LKContentView(R.layout.fragment_end_buying)
public class BuyingEndFragment extends MyBaseFragment {

    /**
     * 列表
     */
    @LKInjectView(R.id.lv_buying_end)
    private ListView lvBuyingEnd;

    @LKInjectView(R.id.ref_layout)
    private SmartRefreshLayout ref_layout;

    @LKInjectView(R.id.ll_empty_view)
    private LinearLayout ll_empty_view;

    private SnatchListAdapter mAdapter;

    private String token;


    private int limit = 20;

    private int offset = 1;


    @Override
    protected void initView() {
        super.initView();

        mAdapter = new SnatchListAdapter(getContext(), SnatchListAdapter.BUYING_ENDING);
        lvBuyingEnd.setAdapter(mAdapter);

        ref_layout.setEnableRefresh(true);//启用刷新
        ref_layout.setEnableLoadmore(true);//启用加载

    }

    @Override
    protected void initData() {
        super.initData();
        token = PreferencesUtil.getInstance().getString(getContext(), Constant.TOKEN);
    }

    @Override
    protected void initEvent() {
        super.initEvent();

        ref_layout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                offset++;
                getBuyingData(limit, offset);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                offset = 1;
                ref_layout.setEnableLoadmore(true);
                getBuyingData(limit, offset);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        offset = 1;
        getBuyingData(limit, offset);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mAdapter != null) {
            mAdapter.cancelAllTimers();
        }
    }

    /**
     * 获取抢购中数据
     */
    private void getBuyingData(int limit, int offset) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("limit", String.valueOf(limit));
        map.put("offset", String.valueOf(offset));
        map.put("status", "20");
        LKUtil.getHttpManager().postBody(HttpConstant.GET_SNATCH_PERIOD, map, getSnatchPeriodHander);
    }

    @SuppressLint("HandlerLeak")
    private BaseHttpAsycResponceHandler getSnatchPeriodHander =
            new BaseHttpAsycResponceHandler<SnatchPeriodBean>() {
                @Override
                public void onSuccess(SnatchPeriodBean snatchPeriodBeans) {
                    ref_layout.finishRefresh();
                    ref_layout.finishLoadmore();
                    super.onSuccess(snatchPeriodBeans);
                    if (snatchPeriodBeans != null) {
                        List<SnatchPeriodBean.DataBean> data = snatchPeriodBeans.getData();
                        if (data != null) {
                            if (offset == 1) {
                                mAdapter.updateData(data);
                            } else {
                                mAdapter.addData(data);
                            }
                            //数据不足一页此时数据已经加载完毕
                            //下一页无数据时禁用加载
                            ref_layout.setEnableLoadmore(!(data.size() < limit));
                        } else {
                            ref_layout.setEnableLoadmore(false);
                        }
                    } else {
                        ref_layout.setEnableLoadmore(false);
                    }

                    //缺省页展示
                    ref_layout.setVisibility(mAdapter.getCount() > 0 ? View.VISIBLE : View.GONE);
                    ll_empty_view.setVisibility(mAdapter.getCount() > 0 ? View.GONE : View.VISIBLE);
                }

                @Override
                public void onFailed(int code, String message) {
                    super.onFailed(code, message);
                    ref_layout.finishRefresh();
                    ref_layout.finishLoadmore();
                    if (offset > 1) offset--;
                    //缺省页展示
                    ref_layout.setVisibility(mAdapter.getCount() > 0 ? View.VISIBLE : View.GONE);
                    ll_empty_view.setVisibility(mAdapter.getCount() > 0 ? View.GONE : View.VISIBLE);
                }

                @Override
                public void onError() {
                    super.onError();
                    ref_layout.finishRefresh();
                    ref_layout.finishLoadmore();
                    if (offset > 1) offset--;
                    //缺省页展示
                    ref_layout.setVisibility(mAdapter.getCount() > 0 ? View.VISIBLE : View.GONE);
                    ll_empty_view.setVisibility(mAdapter.getCount() > 0 ? View.GONE : View.VISIBLE);
                }
            };
}
